// pages/user_centre/home/home.js
Component({
  options: {
    addGlobalClass: true,
  },
  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: '',
    nickName: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      var that = this;
      const openid = wx.getStorageSync("openid")
      console.log("In  user_centre", openid)
      const app = getApp()
      console.log("appGlobalData", app.globalData)
      if (openid && app.globalData.userInfo) {
        const myavatarUrl = app.globalData.userInfo.avatarUrl
        const mynickName = app.globalData.userInfo.nickName
        that.setData({
          avatarUrl: myavatarUrl,
          nickName: mynickName
        })
      }

      //获取未读消息数量
      wx.cloud.callFunction({
        // 要调用的云函数名称
        name: 'unreadNum',
        // 传递给云函数的参数
        data: {},
        success: res => {
          // output: res.result === 3
          console.log("unreadNumber succ", res)
          let unread = res.result.count
          unread = (unread > 99) ? '99+' : unread
          that.setData({
            unread: unread
          })
        },
        fail: err => {
          // handle error
          console.log("unreadNumber fail", res)
        },
        complete: () => {
          // ...
        }
      })

    },
    ready() {

    },
  },

  pageLifetimes: {
    show: function () {
      // 页面被展示
      const thiz = this
      //获取消息条数

      //获取未读消息数量
      wx.cloud.callFunction({
        // 要调用的云函数名称
        name: 'unreadNum',
        // 传递给云函数的参数
        data: {},
        success: res => {
          // output: res.result === 3
          console.log("unreadNumber succ", res)
          let unread = res.result.count
          unread = (unread > 99) ? '99+' : unread
          thiz.setData({
            unread: unread
          })
        },
        fail: err => {
          // handle error
          console.log("unreadNumber fail", res)
        },
        complete: () => {
          // ...
        }
      })

    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  },

  methods: {

  }

})